package com.qk.management.service.impl;

import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.management.mapper.DeptMappper;
import com.qk.management.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: hm
 * @Date: 2025/09/29 20:25
 * @Description:
 */
@Service
@SuppressWarnings("all")
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMappper deptMappper;

    @Override
    public List<Dept> getAllDepts() {
        return deptMappper.selectAllDepts();
    }

    @Override
    public void deleteById(Integer id) {
        deptMappper.deleteById(id);
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMappper.update(dept);
    }

    @Override
    public Dept getById(Integer id) {
        Dept dept = deptMappper.selectById(id);
        return dept;
    }

    @Override
    public PageResult<Dept> page(String name, Integer status, Integer page, Integer pageSize) {

        Integer total = deptMappper.count(name, status);

        Integer offset = (page - 1) * pageSize;
        List<Dept> rows = deptMappper.selectByPage(name, status, offset, pageSize);

        return PageResult.<Dept>builder()
                .total(total)
                .rows(rows)
                .build();
    }

    @Override
    public void addDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMappper.addDept(dept);
    }
}
